# app/routes/messages.py
from flask import Blueprint, jsonify, request
from app.services.message_service import MessageService
from app.utils.security import token_required
from app.utils.response_utils import (
    success_response,
    error_response,
    ResponseCode
)

# 创建蓝图对象
messages_bp = Blueprint('messages', __name__)


@messages_bp.route('/getChannelMessageList', methods=['POST'])
@token_required
def get_channel_messages(currentUserId):
    """获取频道消息历史"""
    data = request.get_json()
    channel_id = data.get('channel_id')
    server_id = data.get('server_id')
    userType = data.get('userType')
    pageSize = data.get('pageSize',20)
    pageCur = data.get('pageCur',1)

    if not channel_id or not userType or not server_id:
        return error_response(
            code=ResponseCode.PARAM_ERROR,
            message='参数错误'
        )

    result = (MessageService
        .getChannelMessages(channel_id=channel_id,user_id=currentUserId,
                            server_id=server_id,userType=userType,
                            pageSize=pageSize,pageCur=pageCur))


    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            code=ResponseCode.PERMISSION_DENIED,
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        data=result,
        message=f'success'
    )


# @messages_bp.route('/sendChannelMessage', methods=['POST'])
# @token_required
# def send_channel_message(currentUserId):
#     """在频道中发送消息"""
#     user_id = currentUserId
#     data = request.get_json()
#     content = data.get('content')
#     channel_id = data.get('channel_id')
#
#     if not content:
#         return error_response(
#             code=ResponseCode.PARAM_ERROR,
#             message='输入为空'
#         )
#
#     # 检查用户是否有权限在该频道发送消息
#     # 这里需要实现权限检查逻辑
#
#     message = MessageService.create_channel_message(
#         channel_id=channel_id,
#         sender_id=user_id,
#         content=content
#     )
#
#     if isinstance(message, tuple):  # 如果是错误响应
#         return error_response(
#             code=ResponseCode.PERMISSION_DENIED,
#             message=message[0],
#             http_code=message[1]
#         )
#
#     # 通过WebSocket广播新消息
#     from app.sockets import broadcast_new_message
#     broadcast_new_message(message)
#
#     return success_response(message='消息发送成功',data={'message':message})

# @messages_bp.route('/deleteMessage', methods=['POST'])
# @token_required
# def delete_message(currentUserId):
#     """删除消息接口"""
#     data = request.get_json()
#     message_id = data.get('message_id')
#
#     if not message_id:
#         return error_response(
#             code=ResponseCode.PARAM_ERROR,
#             message='消息ID不能为空'
#         )
#
#     success = MessageService.delete_message(
#         message_id=message_id,
#         user_id=currentUserId
#     )
#
#     if isinstance(success, tuple):  # 如果是错误响应
#         return error_response(
#             code=ResponseCode.PERMISSION_DENIED,
#             message=success[0],
#             http_code=success[1]
#         )
#     from app.sockets import broadcast_delete_message
#     broadcast_delete_message(success)
#
#     return success_response(message='删除成功')


@messages_bp.route('/getPrivateChatId', methods=['POST'])
@token_required
def get_private_chat_id(currentUserId):
    data = request.get_json()
    friend_id = data.get('friend_id')
    if not friend_id:
        return error_response(
            code=ResponseCode.PARAM_ERROR,
            message='参数错误'
        )

    chat_id = MessageService.getPrivateChatId(user_id=currentUserId,friend_id=friend_id)

    if isinstance(chat_id, tuple):  # 如果是错误响应
        return error_response(
            code=ResponseCode.PERMISSION_DENIED,
            message=chat_id[0],
            http_code=chat_id[1]
        )

    return success_response(
        data={
            'chat_id':chat_id
        },
        message=f'success'
    )


@messages_bp.route('/getPrivateMessageList', methods=['POST'])
@token_required
def get_private_messages(currentUserId):
    """获取频道消息历史"""
    data = request.get_json()
    chat_id = data.get('chat_id')
    pageSize = data.get('pageSize',20)
    pageCur = data.get('pageCur',1)

    if not chat_id:
        return error_response(
            code=ResponseCode.PARAM_ERROR,
            message='参数错误'
        )

    result = (MessageService
        .getPrivateMessages(chat_id=chat_id,user_id=currentUserId,
                            pageSize=pageSize,pageCur=pageCur))


    if isinstance(result, tuple):  # 如果是错误响应
        return error_response(
            code=ResponseCode.PERMISSION_DENIED,
            message=result[0],
            http_code=result[1]
        )

    return success_response(
        data=result,
        message=f'success'
    )


# @messages_bp.route('/sendPrivateMessage', methods=['POST'])
# @token_required
# def send_private_message(currentUserId):
#     """在频道中发送消息"""
#     user_id = currentUserId
#     data = request.get_json()
#     content = data.get('content')
#     direct_chat_id = data.get('direct_chat_id')
#
#     if not content:
#         return error_response(
#             code=ResponseCode.PARAM_ERROR,
#             message='输入为空'
#         )
#
#     # 检查用户是否有权限在该频道发送消息
#     # 这里需要实现权限检查逻辑
#
#     message = MessageService.create_channel_message(
#         direct_chat_id=direct_chat_id,
#         sender_id=user_id,
#         content=content
#     )
#
#     if isinstance(message, tuple):  # 如果是错误响应
#         return error_response(
#             code=ResponseCode.PERMISSION_DENIED,
#             message=message[0],
#             http_code=message[1]
#         )
#
#     # 通过WebSocket广播新消息
#     from app.sockets import broadcast_new_message
#     broadcast_new_message(message)
#
#     return success_response(message='消息发送成功',data={'message':message})